package com.glory.dgcpcpda.activity.check;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.CheckSheetEntity;
import com.glory.dgcpcpda.entity.MLTlist;
import com.glory.dgcpcpda.entity.MLotToolEntity;
import com.glory.dgcpcpda.entity.SheetInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NewCheckTableActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvState)
    TextView tvState;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvCheckTime)
    TextView tvCheckTime;
    @BindView(R.id.tvClazz)
    TextView tvClazz;
    @BindView(R.id.tvlotID)
    TextView tvlotID;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvCheckName)
    TextView tvCheckName;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llLocation)
    LinearLayout llLocation;

    private CheckSheetEntity checkSheetEntity = new CheckSheetEntity();
    private String mObjrctRRN = "";
    private MLotToolEntity mLotTool = new MLotToolEntity();
    private static final String Start  = "Start";
    private static final String EqpSubStateMor  = "MOR";
    private Boolean IsStart = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_new_check_table;
    }

    @Override
    protected void initView() {
        tvTitle.setText("交接班检");
        tvBaseSubmit.setText("下一步");
        tvState.setVisibility(View.VISIBLE);
        llLocation.setVisibility(View.VISIBLE);
        setMainLayout(llMain);
        String eqpid = getIntent().getExtras().getString("EQPID");
        mObjrctRRN = getIntent().getExtras().getString("OBJECTRRN");
        if (eqpid!= null){
            etEqpWork.setText(eqpid);
            requestCheckInfo(eqpid, "");
        }
        initEdit(etEqpWork, s -> {
            requestCheckInfo(s, "");
            getMlotTool(s);
        });
    }

    @Override
    protected void initData() {
    }

    private void getMlotTool(String eqpid) {
        FreeUI_EntityCallBack<BaseResponseBean<MLotToolEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MLotToolEntity>>
                (new TypeToken<BaseResponseBean<MLotToolEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    mLotTool = response.body().getResponse().getBody();
                    return;
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "MESLTlist");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpid);
        NetUtils.requestPostCPC("CPC.MESLTlist", "MESLTlist", this, map, callBack);
    }

    public void requestCheckInfo(String s, String action){
        FreeUI_EntityCallBack<BaseResponseBean<CheckSheetEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<CheckSheetEntity>>
                (new TypeToken<BaseResponseBean<CheckSheetEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    checkSheetEntity = response.body().getResponse().getBody();
                    if(Start.equals(action)){
                        requestTrackinLogEvent();
                        return;
                    }
                    if(EqpSubStateMor.equals(checkSheetEntity.getEQPSUBSTATE())){
                        IsStart = true;
                    }
                    tvCustomCode.setText(checkSheetEntity.getCUSTOMERCODE());
                    tvlotID.setText(checkSheetEntity.getLOTID());
                    tvPackageType.setText(checkSheetEntity.getPACKAGETYPE());
                    tvPartName.setText(checkSheetEntity.getPARTNAME());
                    tvClazz.setText(checkSheetEntity.getSHIFT());
                    tvEqpType.setText(checkSheetEntity.getEQUIPMENTTYPE());
                    tvLocation.setText(checkSheetEntity.getLOCATION());
                    tvCheckTime.setText(checkSheetEntity.getSHEETTIME());
                    if(checkSheetEntity.getSHEETLIST().size() > 0){
                        tvCheckName.setText(checkSheetEntity.getSHEETLIST().get(0).getNAME());
                        tvStep.setText(checkSheetEntity.getSHEETLIST().get(0).getSTEPNAME());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"CHECKSHEET");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERY");
        map.addProperty("EQUIPMENTID",s);
        map.addProperty("CHECKSHEETDATARRN",mObjrctRRN);
        map.addProperty("CHECKTIMING","Daily_Change");
        NetUtils.requestPostNet("CPC.CHECKSHEET","CHECKSHEET",NewCheckTableActivity.this,map,callBack);
    }

    public void requestTrackinLogEvent(){
        FreeUI_EntityCallBack<BaseResponseBean<MLotToolEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MLotToolEntity>>
                (new TypeToken<BaseResponseBean<MLotToolEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("操作成功",true);

                    IsStart = true;
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TRACKINLOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "TRACKINLOGEVENT");
        map.addProperty("EQUIPMENTID", getText(etEqpWork));
        map.addProperty("COMMENT", "交接班检");
        NetUtils.requestPostNet("CPC.TRACKINLOGEVENT", "TRANSFERMES", this, map, callBack);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tvBaseSubmit,R.id.tvState})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (!IsStart){
                    showToast("当前设备未开始",false);
                    return;
                }

                if (actionControl("NewCheck")){
                    return;
                }

                if (checkSheetEntity.getSHEETLIST().size() <= 0){
                    showToast("未获取到点检数据",false);
                    return;
                }

                if (getText(etEqpWork).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }

                // 在切换试图之前把值赋值好
                List<SheetInfoEntity.SheetCheckTableLineEntity> sheetchecktablelines = checkSheetEntity.getSHEETLIST().get(0).getSHEETCHECKTABLELINES();
                List<SheetInfoEntity.SheetCheckTableLineEntity> newSheetlines = new ArrayList<>();
                for (SheetInfoEntity.SheetCheckTableLineEntity info:sheetchecktablelines) {
                    if(mLotTool != null){
                        // 丝材型号及线径
                        if(info.getNAME().equals("丝材型号及线径")){
                            MLTlist[] mlTlist = mLotTool.getMLTlist();
                            if(mlTlist != null){
                                for (MLTlist ml:mlTlist) {
                                    if(ml.getMATERIAL_DESC().contains("丝材")){
                                        info.setVALUE(ml.getSPEC1());
                                    }
                                }
                            }
                        } else if(info.getNAME().equals("劈刀型号")){
                            MLTlist[] mlTlist = mLotTool.getMLTlist();
                            if(mlTlist != null){
                                for (MLTlist ml:mlTlist) {
                                    if(ml.getMATERIAL_DESC().contains("劈刀")){
                                        info.setVALUE(ml.getSPEC1());
                                    }
                                }
                            }
                        }else if(info.getNAME().equals("吸嘴型号")){
                            MLTlist[] mlTlist = mLotTool.getMLTlist();
                            if(mlTlist != null){
                                for (MLTlist ml:mlTlist) {
                                    if(ml.getMATERIAL_DESC().contains("吸嘴")){
                                        info.setVALUE(ml.getSPEC1());
                                    }
                                }
                            }
                        }else if(info.getNAME().equals("顶针型号")){
                            MLTlist[] mlTlist = mLotTool.getMLTlist();
                            if(mlTlist != null){
                                for (MLTlist ml:mlTlist) {
                                    if(ml.getMATERIAL_DESC().contains("顶针")){
                                        info.setVALUE(ml.getSPEC1());
                                    }
                                }
                            }
                        }else if(info.getNAME().equals("装片胶型号")){
                            MLTlist[] mlTlist = mLotTool.getMLTlist();
                            if(mlTlist != null){
                                for (MLTlist ml:mlTlist) {
                                    if(ml.getMATERIAL_DESC().contains("装片胶")){
                                        info.setVALUE(ml.getSPEC1());
                                    }
                                }
                            }
                        }
                        newSheetlines.add(info);
                    }
                }
                newSheetlines.sort((Sheet1, Sheet2) -> {
                    return Sheet1.getOBJECTRRN().compareTo(Sheet2.getOBJECTRRN());
                });
                checkSheetEntity.getSHEETLIST().get(0).setSHEETCHECKTABLELINES(newSheetlines);
                Bundle bundle = new Bundle();
                bundle.putSerializable("SHEETDATA", checkSheetEntity);
                bundle.putString("OBJECTRRN",mObjrctRRN);
                bundle.putString("CHECKTIME","交接班检");
                bundle.putString("EQUIPMENTID",getText(etEqpWork));
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,CheckDescActivity.class);
                finish();
                break;
            case R.id.tvState:
                if (getText(etEqpWork).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }

                requestCheckInfo(getText(etEqpWork), Start);
                break;
        }
    }


}
